
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>JDK8后HashMap为什么要改成尾插发 - 学习卡片</title>
      <style>
        body { font-family: sans-serif; background-color: #f0f8ff; color: #333; display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
        .header h1 { font-size: 32px; }
        .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; width: 100%; max-width: 1200px; }
        .card-container { perspective: 1200px; cursor: pointer; height: 250px; }
        .card { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transition: transform 0.7s; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
        .card-container.flipped .card { transform: rotateY(180deg); }
        .card-face { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; flex-direction: column; box-sizing: border-box; border-radius: 16px; background-color: #fff; padding: 24px; }
        .card-back { background-color: #f0fff4; transform: rotateY(180deg); justify-content: space-between; }
        .card-category { font-size: 14px; color: #0052d9; margin-bottom: 8px; font-weight: 500; }
        .card-question { font-size: 20px; font-weight: 500; flex-grow: 1; display: flex; align-items: center; justify-content: center; text-align: center; }
        .card-answer-wrapper { flex-grow: 1; overflow-y: auto; }
        .card-answer { font-size: 15px; line-height: 1.7; }
        .card-footer { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 16px; margin-top: 16px; }
        .card-source { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 12px; margin-top: 12px; }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>JDK8后HashMap为什么要改成尾插发 - 学习卡片</h1>
      </div>
      <div class="grid-container">
        
    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">在处理哈希冲突时，JDK 8 版本的 HashMap 与之前的版本在链表插入节点的方式上有何不同？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">在 JDK 8 之前，HashMap 在处理哈希冲突时采用头插法，即新节点插入链表的头部。而在 JDK 8 之后，改为了尾插法，即在链表尾部插入新节点。</div>
          </div>
          <div class="card-source">来源: 文档开篇段落</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">为什么说 JDK 8 之前的 HashMap 在多线程环境下使用头插法可能导致死循环？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">因为 HashMap 并非线程安全，当两个线程在同一个链表上同时执行头插入操作时，如果执行顺序不当，可能会导致链表形成环形结构（循环链表）。这会使得后续的遍历和查找操作陷入死循环，导致应用卡死。</div>
          </div>
          <div class="card-source">来源: 1. 防止死循环</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">优化</div>
          <div class="card-question">JDK 8 对 HashMap 在哈希冲突严重时做了什么关键优化？这个优化的触发条件是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">优化</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">JDK 8 引入了红黑树。当链表的长度超过一个特定阈值（默认为 8）时，链表会转换为红黑树。这一优化将查找效率从 O(n) 提升到了 O(log n)。</div>
          </div>
          <div class="card-source">来源: 2. 优化查找效率</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">与头插法相比，尾插法在 HashMap 扩容（rehash）时有什么优势？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">使用尾插法后，在扩容时重新分配元素的位置顺序与原始顺序保持一致。这种顺序的稳定性减少了对链表顺序的打乱，从而优化了扩容操作的性能。</div>
          </div>
          <div class="card-source">来源: 3. 提高扩容效率</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">优化</div>
          <div class="card-question">为什么说尾插法更有利于 JDK 8 中 HashMap 将链表转换为红黑树的操作？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">优化</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">因为在采用尾插法插入新元素时，链表中元素的相对顺序更为稳定。这种稳定性使得链表在转换为红黑树时，结构更容易处理，并且有助于保持红黑树的平衡结构。</div>
          </div>
          <div class="card-source">来源: 2. 优化查找效率</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">除了性能和并发问题，从代码维护的角度看，HashMap 改用尾插法还有什么好处？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">尾插法的实现更为简单和直观，它避免了头插法可能引起的链表反转、顺序混乱等复杂问题。由于链表顺序更稳定，代码的可读性和可维护性也得到了提高。</div>
          </div>
          <div class="card-source">来源: 4. 简化代码和维护</div>
        </div>
      </div>
    </div>

      </div>
    </body>
    </html>
